package com.tony.pmp.server.common;

import com.google.common.base.Strings;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: WebOperationService
 * @Auther: 皮蛋布丁
 * @Date: 2021/08/03/21:58
 * @Description: 通用的web操作服务
 */
@Service
public class WebOperationService {

    private static final Logger log = LoggerFactory.getLogger(WebOperationService.class);

    /**
    * @Description: downloadFile 通用下载插件
    * @Param: [response, inputStream, fileName]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/8/3 22:09
    */
    public void downloadFile(HttpServletResponse response, InputStream inputStream,String fileName) throws Exception{
        if (inputStream == null || Strings.isNullOrEmpty(fileName)) {
            return;
        }

        BufferedInputStream bufferedInputStream = null;
        OutputStream outputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(inputStream);
            outputStream = response.getOutputStream();
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso-8859-1"));
            byte[] buffer = new byte[10240];
            int length = bufferedInputStream.read(buffer);
            while (length != -1) {
                bufferedOutputStream.write(buffer,0,length);
                length = bufferedInputStream.read(buffer);
            }
            bufferedOutputStream.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (Exception e) {}
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {}
            }
        }
    }

    /**
    * @Description: downloadExcel 下载Excel
    * @Param: [response, workbook, fileName]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/8/3 22:10
    */
    public void downloadExcel(HttpServletResponse response, Workbook workbook,String fileName) throws Exception{
        response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso-8859-1"));
        response.setContentType("application/ynd.ms-excel;charset=UTF-8");
        OutputStream out=response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
    }

}
